﻿using System.Linq;
using www.Models;

namespace Data.DAO
{
    public class TypeValueDAO//:BaseDAO<RELAXEntities,RelaxType>
   {
       #region value
       private RelaxDataContext _context = new RelaxDataContext();
        TypeRefDAO _typeRefDao=new TypeRefDAO();
       #endregion

       #region Hàm lấy dữ liệu

       #endregion
       #region Add
       public void Add(TYPE_VALUE type)
       {
           _context.TYPE_VALUEs.InsertOnSubmit(type);
           _context.SubmitChanges();

       }
       #endregion
       #region Update

       public bool Update(TYPE_VALUE type)
       {
           var first = _context.TYPE_VALUEs.FirstOrDefault(p => p.languagecode == type.languagecode && p.typeid==type.typeid);
           if (first != null)
           {
               first.name = type.name;
               first.tag = type.tag;
               first.decription = type.decription;
               _context.SubmitChanges();
               return true;
           }
           return false;
       }
       #endregion

       #region Delete
       public void Delete(long iId, long iLanguageCode)
       {
           var lData = _context.TYPE_VALUEs.Where(p => p.typeid == iId);
           var delete = lData.Count() == 1 || iLanguageCode == 0;
           if (iLanguageCode != 0)
               lData = lData.Where(p => p.languagecode == iLanguageCode);
           foreach (var item in lData)
           {
               _context.TYPE_VALUEs.DeleteOnSubmit(item);
           }
           _context.SubmitChanges();
          if(delete)
              _typeRefDao.Delete(iId);
       }
       #endregion
   }
}
